/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "blinker.h"
#include "conuisurfacelogger.h"

static const TUint KBlinkIntervalMicroseconds = 500000;

CBlinker* CBlinker::NewL(CConUiSurface& aSurface)
    {
    FL(_L("CBlinker::NewL"));
    CBlinker* self = new(ELeave) CBlinker(aSurface);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
    }

CBlinker::CBlinker(CConUiSurface& aSurface) : CTimer(CActive::EPriorityStandard),
    iSurface(aSurface)
    {}

void CBlinker::ConstructL()
    {
    FL(_L("CBlinker::ConstructL"));
    CTimer::ConstructL();
    After(KBlinkIntervalMicroseconds);
    }

CBlinker::~CBlinker()
    {
    Cancel();
    iBlinkingRegion.Close();
    }

void CBlinker::SetBlinkingL(const TRect& aRect, TBool aBlinking)
    {
    FL(_L("CBlinker::SetBlinkingL"));
    FLTRACEVALUE(aRect);
    if (aBlinking)
        {
        iBlinkingRegion.AddRect(aRect);
        }
    else
        {
        iBlinkingRegion.SubRect(aRect);
        }
    CheckRegionErrorL();
    }

void CBlinker::SetBlinkingL(const RRegion& aRegion, TBool aBlinking)
    {
    FL(_L("CBlinker::SetBlinkingL"));
    if (aBlinking)
        {
        iBlinkingRegion.Union(aRegion);
        }
    else
        {
        iBlinkingRegion.SubRegion(aRegion);
        }
    CheckRegionErrorL();
    }

void CBlinker::CheckRegionErrorL()
    {
    FL(_L("CBlinker::CheckRegionErrorL"));
    if (iBlinkingRegion.CheckError())
        {
        iBlinkingRegion.Clear();
        User::Leave(KErrNoMemory);
        }
    }

void CBlinker::ApplyBlinkingL(CDrawingBuffer::TBlinkMode aMode)
    {
    FL(_L("CBlinker::ApplyBlinkingL"));
    FLTRACEVALUE(aMode);
    iSurface.iBuffer->SetBlinkMode(aMode);
    iSurface.ValidateL(iBlinkingRegion);
    }

void CBlinker::RunL()
    {
    FL(_L("CBlinker::RunL"));
    FLTRACEVALUE(iStatus.Int());
    TInt err = iStatus.Int();
    if (KErrCancel != err)
        {
        After(KBlinkIntervalMicroseconds);
        User::LeaveIfError(err);
        // toggle blink mode
        CDrawingBuffer::TBlinkMode mode;
        if (CDrawingBuffer::EBlinkInvisible == iSurface.iBuffer->BlinkMode())
            {
            mode = CDrawingBuffer::EBlinkVisible;
            }
        else
            {
            mode = CDrawingBuffer::EBlinkInvisible;
            }
        ApplyBlinkingL(mode);
        }
    }

TInt CBlinker::RunError(TInt aError)
    {
    FL(_L("CBlinker::RunError"));
    FLTRACEVALUE(aError);
    TRAP_IGNORE(ApplyBlinkingL(CDrawingBuffer::EBlinkVisible));
    return KErrNone;
    }

void CBlinker::DoCancel()
    {
    FL(_L("CBlinker::DoCancel"));
    // even if this call leaves it sets blink mode to correct
    // next succesful redraw will update engine properly
    TRAP_IGNORE(ApplyBlinkingL(CDrawingBuffer::EBlinkVisible));
    }
